//
//  UIDevice+RWAdditions.m
//  iconstruction-ios
//
//  Created by 81SK on 2023/2/9.
//

#import "UIDevice+RWAdditions.h"
#import <RWCKeyChainStore/RWUICKeyChainStore.h>

static BOOL pad;

static RWUICKeyChainStore *_keyChainStore;

NSString *const kKeychainStoreServiceKey = @"com.cscec81.iconstruction.values.keychain.store.service.key.www";
NSString *const kUUIDKey = @"com.cscec81.iconstruction.uuid.key";

@implementation UIDevice (RWAdditions)

- (NSString * )loadDeviceToken {
    return @"";
}

- (NSString *)rw_uuid {
    //先取,后存
    NSString *result = [self.keyChainStore stringForKey:kUUIDKey];
    if (result == nil || result.length < 1) {
        NSString *uuidVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
        result = [uuidVendor stringByReplacingOccurrencesOfString:@"-" withString:@""];
        //存三次
        BOOL success = [self.keyChainStore setString:result forKey:kUUIDKey];
        if (success == NO) {
            success = [self.keyChainStore setString:result forKey:kUUIDKey];
        }
        if (success == NO) {
            [self.keyChainStore setString:result forKey:kUUIDKey];
        }
    }
    return result;
}


- (BOOL)rw_isPhone {
    static dispatch_once_t one;
    dispatch_once(&one, ^{
        if (@available(iOS 13.0, *)) {
            pad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone;
        } else {
            pad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone;
        }
    });
    return pad;
}

- (BOOL)rw_isIPhoneXSeries {
    BOOL isIPhoneHeight = NO;
    // 根据安全区域判断
    if (@available(iOS 11.0, *)) {
        CGFloat height = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom;
        isIPhoneHeight = height > 0;
    }
    return [self rw_isPhone] && isIPhoneHeight;
}

- (BOOL)rw_scale {
    return [UIScreen mainScreen].scale;
}

#pragma mark - *****************************************  屏幕尺寸相关  *********************************************

- (CGFloat)rw_sreenWidth {
    return [[UIScreen mainScreen] bounds].size.width;
}

- (CGFloat)rw_sreenHeight {
    return [[UIScreen mainScreen] bounds].size.height;
}

- (CGFloat)rw_sreenMaxLength {
    return MAX(self.rw_sreenWidth, self.rw_sreenHeight);
}

- (CGFloat)rw_sreenMinLength {
    return MIN(self.rw_sreenWidth, self.rw_sreenHeight);
}

- (CGFloat)rw_statusBarHeight {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
        return statusBarManager.statusBarFrame.size.height;
    } else {
        return [UIApplication sharedApplication].statusBarFrame.size.height;
    }
}

- (CGFloat)rw_navigationBarHeight {
    return 44.0;
}

- (CGFloat)rw_naviTopSafeValue {
    return [self rw_statusBarHeight] + [self rw_navigationBarHeight];
}

- (CGFloat)rw_naviTopSpacingOffsetValue {
    return [self rw_isIPhoneXSeries] ? (44 - 20) : 0;
}

- (CGFloat)rw_tabBarHeight {
    return (49.0 + [self rw_tabbarBottomSafeValue]);
}

- (CGFloat)rw_tabbarBottomSafeValue {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.bottom;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.bottom;
    }
    return 0;
//    return [self rw_isIPhoneXSeries] ? (34) : 0;
}

#pragma mark - *****************************************  APP相关  *********************************************

/// APP名称
- (NSString *)rw_bundleDisplayName {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
}

/// APP包名
- (NSString *)rw_bundleIdentifier {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
}

/// APP版本号
- (NSString *)rw_bundleShortVersion {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

/// APP build号
- (NSString *)rw_bundleVersion {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
}


#pragma mark - *****************************************  Getter  *********************************************

- (RWUICKeyChainStore *)keyChainStore {
    if (!_keyChainStore) {
        [RWUICKeyChainStore setDefaultService:kKeychainStoreServiceKey];
        _keyChainStore = [RWUICKeyChainStore keyChainStoreWithService:kKeychainStoreServiceKey];
    }
    return _keyChainStore;
}

@end
